preUri = "https://flow-openapi.teambition.com/pipeline/webhook/"
webhooks = [
        'whale-gateway': 'vl3AwEaKcgUoeVDk7rp0',
        'whale-open': 'uvly5nw7CnhrlHIaMTQD',
        'whale-ops/whale-ops-admin': 'o8a4SWcKKOxV3xukZuok',
        'whale-pyxj': 'AAy76Wcztidk4bPfX0xa',
        'whale-ets/whale-ets-main': 'eW1Bdpm3GXXnaZYutEVE',
        'whale-ets/whale-ets-product': 'z3rtgr4bnFNsGHz6jBzJ',
        'whale-ets/whale-ets-user': 'A1RxK4zhi2NnYJuPF9zE',
        'whale-ets/whale-ets-workform': 'lVH3zWQQ2CkCd4z9UwlS',
        'whale-basics/whale-base-dbsubscribe': '4fhsqZLHyBsqma3bvgwe',
        'whale-basics/whale-base-excel': 'sHUxXljFuFz4LXNwHWHb',
        'whale-basics/whale-base-job': 'YHz07ka00p5OMbrX4LKC',
        'whale-basics/whale-base-logs': '1DwGDoBnJ6uVf0LKTw02',
        'whale-basics/whale-base-notification': 'hywVBS7gLakEAwXaEWVk',
        'whale-sps/whale-sps-user': 'kfzD7pjgyEjU0OxYlmst',
        'whale-sps/whale-sps-workform': '12eFmZDxARAmtXH2GTab'
]

pipeline {
    agent any
    tools {
        maven "maven3.8.6"
    }


    stages {
        stage('拉取代码') {
            agent none
            steps {
                git branch: 'prod', credentialsId: '64e32452-a772-4e6f-850d-4635ce21848c', url: 'http://git.pyxjiang.com/whale-group/server/whale-parent.git'

            }
        }

        stage('编译-打包-镜像') {
            agent none
            steps {
                sh '''
					mvn -B -s /var/jenkins_home/maven_setting/settings.xml clean package -T 1C -Dmaven.test.skip=true -Dautoconfig.skip  -pl ${appNames} -am
				'''
                script {
                    imagesRoot = "registry-vpc.cn-shenzhen.aliyuncs.com/pyxj-whale";
                    for(path in env.appNames.tokenize(',')){
                        path = path.replace('"', '');
                        appName = path;

                        if(path.contains("/")) {
                            appName = path.split("/")[1];
                        }
                        sh "pwd"
                        sh "mv ${path}/target/${appName}.jar ${path}/target/app.jar"
                        sh "cp Dockerfile ${path}/target/"
                        sh "cp agent.jar ${path}/target/"
                        withDockerRegistry(credentialsId: 'dockerRegister', toolName: 'docker', url: "https://${imagesRoot}") {
                            image = docker.build("${imagesRoot}/${appName}:v_${BUILD_NUMBER}", "${path}/target/")
                            image.push();
                        }
                    }
                }
            }
        }

        stage("发布") {
            agent none
            steps {
                input message: '发布？', ok: '确认', submitter: 'admin'
                script {
                    for(path in env.appNames.tokenize(',')){
                        path = path.replace('"', '');
                        webhook = getWebhook(path);
                        echo "${webhook}"
                        sleep time: 5, unit: 'SECONDS'
                        sh "sh /var/jenkins_home/tools/shell/curl.sh ${webhook} v_${BUILD_ID}"
                    }
                }
            }
        }

        stage('查看镜像版本') {
            agent none
            steps {
                script {
                    imagesRoot = "registry-vpc.cn-shenzhen.aliyuncs.com/pyxj-whale";
                    echo "打包镜像版本号：v_${BUILD_NUMBER}"
                    for(path in env.appNames.tokenize(',')){
                        appName = path;
                        if(path.contains("/")) {
                            appName = path.split("/")[1];
                        }
                        echo "镜像全路径：${imagesRoot}/${appName}:v_${BUILD_NUMBER}"
                    }
                }
            }
        }
    }
}

def getWebhook(String path) {
    return preUri + webhooks.getAt(path)
}